//app.js
let utils = require('utils/api.js');
let base64 = require('utils/base64.js');
App({
  globalData: {
    requestUrl: 'https://wangdao.natappvip.cc/miniProgramComm', //服务器请求地址
    userInfo: null,
    openid: null,
    rd_session: null,
    btcexchange: null,
    machineId: null,
    serverSessionKey: null,
    payType: '2', //1:支付宝 2:微信 3:魔彩币
    molotPrice: null, //个人魔彩币
    apiSecret: 'ZMJSmVsT1XxreyP8N32c7kRWtJFLVS2g',
    urlUnicode: null,

  },
  onLaunch: function () {
    var that = this
    // try {
    //   let value = wx.getStorageSync('serverSessionKey')
    //   if (value) {
    //     console.log('value:'+value)
    //   }
    // } catch (e) {
    //   // Do something when catch error
    // }
    //程序启动获取sessionKey
    wx.checkSession({
      success: function () {
        console.log('session is valid')
        if (!wx.getStorageSync('serverSessionKey')) {
          wx.login({
            success: function (res) {
              if (res.code) {
                //发起网络请求拿serverSessionKey
                that.userLogin(res)
              } else {
                console.log('获取用户登录态失败！' + res.errMsg)
              }
            }
          })
        }
      },
      fail: function () {
        console.log('session is unvalid')
        wx.login({
          success: function (res) {
            if (res.code) {
              //发起网络请求拿serverSessionKey
              that.userLogin(res)
            } else {
              console.log('获取用户登录态失败！' + res.errMsg)
            }
          }
        })
      }
    })
    wx.getUserInfo({
      success: function (res) {
        console.log('interface: wx:getUserInfo')
        console.log(res.userInfo)
      },
      complete: function (res) {
        that.globalData.userInfo = res.userInfo
      }
    })
    // console.log(that.globalData)
  },
  userLogin: function (res) {
    var that = this
    wx.request({
      url: that.globalData.requestUrl + '/login',
      method: "post",
      data: {
        paramList: base64.CusBASE64.encoder('{"code":"' + res.code + '"}')
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // console.log(res);
        if (res.statusCode == 200) { //判断wx.request请求是否成功
          if (res.data) {
            var dat = utils.stringToJson(utils.ascii2native(res.data))
            console.log("interface:login")
            console.log(dat)
            if (dat.status == 0) {
              if (dat.dataList.serverSessionKey) {
                getApp().globalData.serverSessionKey = dat.dataList.serverSessionKey
                try {
                  wx.setStorageSync('serverSessionKey', getApp().globalData.serverSessionKey)
                } catch (e) {
                }
              } else {//获取serverKey失败
              }
            } else { //服务器返回数据错误
            }
          } else {//服务器没有返回数据
          }
        } else {//请求失败
          // console.log(111)
        }
      },
      fail: function (res) {
      },
    })
  }
})